package com.corpgovernment.organization.dataloader;

import com.corpgovernment.api.applytrip.apply.request.ApplyTripRequest;
import com.corpgovernment.api.applytrip.soa.ApplyTripClient;
import com.corpgovernment.api.applytrip.soa.request.ApplyTripPersonEffectRequest;
import com.corpgovernment.api.applytrip.soa.request.ApplyTripPersonRequest;
import com.corpgovernment.api.applytrip.soa.request.ApplyTripUpdateRemakeRequest;
import com.corpgovernment.api.applytrip.soa.request.QueryApplyTripStandardRequest;
import com.corpgovernment.api.applytrip.soa.response.ApplyTripDetailResponse;
import com.corpgovernment.api.applytrip.soa.response.ApplyTripPersonResponse;
import com.corpgovernment.api.applytrip.soa.response.QueryApplyTripStandardResponse;
import com.corpgovernment.api.applytrip.soa.response.TripPersonEffectResponse;
import com.corpgovernment.api.applytrip.vo.AoApplyTripTempVo;
import com.corpgovernment.common.base.BaseUserInfo;
import com.corpgovernment.common.base.JSONResult;
import com.ctrip.corp.obt.generic.core.context.UserInfoContext;
import com.ctrip.corp.obt.generic.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Component
@Slf4j
public class ApplyTripClientLoader {

    @Autowired
    private ApplyTripClient applyTripClient;

    /**
     * 查询出行人
     *
     * @return 出行人
     */
    public List<TripPersonEffectResponse> listEffectTripPerson(ApplyTripPersonEffectRequest request) {
        if (request == null) {
            return new ArrayList<>();
        }
        JSONResult<List<TripPersonEffectResponse>> result = applyTripClient.listEffectTripPerson(request);
        if (result == null || result.getData() == null) {
            log.error("查询出行人异常:" + Optional.ofNullable(result).map(JSONResult::getMsg).orElse("接口无响应"));
            return new ArrayList<>();
        }
        return result.getData();
    }

    /**
     * 出差申请模板列表
     *
     * @param orgId
     * @return
     */
    public List<AoApplyTripTempVo> listApplyTripTempList(String orgId) {
        if (StringUtils.isBlank(orgId)) {
            return new ArrayList<>();
        }
        JSONResult<List<AoApplyTripTempVo>> result = applyTripClient.listApplyTripTempList(orgId);
        if (result == null || result.getData() == null) {
            log.error("出差申请模板列表异常:" + Optional.ofNullable(result).map(JSONResult::getMsg).orElse("接口无响应"));
            return new ArrayList<>();
        }
        return result.getData();
    }

    /**
     * 获取出差申请单的模板
     *
     * @param request
     * @return
     */
    public AoApplyTripTempVo getApplyTemp(ApplyTripRequest request) {
        if (request == null) {
            return null;
        }
        JSONResult<AoApplyTripTempVo> result = applyTripClient.getApplyTemp(request);
        if (result == null || result.getData() == null) {
            log.error("出差申请模板列表异常:" + Optional.ofNullable(result).map(JSONResult::getMsg).orElse("接口无响应"));
            return null;
        }
        return result.getData();
    }


    /**
     * 出差申请单是否需要校验出行人
     *
     * @param applyNo
     * @return
     */
    public Boolean needVerifyPerson(String applyNo) {

        BaseUserInfo baseUserInfo = UserInfoContext.getContextParams(BaseUserInfo.class);
        ApplyTripRequest applyTripRequest = new ApplyTripRequest();
        applyTripRequest.setApplyNo(applyNo);
        applyTripRequest.setCorpId(baseUserInfo.getCorpId());
        AoApplyTripTempVo applyTemp = getApplyTemp(applyTripRequest);
        if (applyTemp != null && 2 == applyTemp.getTripPerson()) {
            return  Boolean.TRUE;
        }
        return Boolean.FALSE;
    }




    /**
     * 查询出差申请单详情
     *
     * @param applyTripNo
     * @return
     */
    public ApplyTripDetailResponse getApplyTripDetail(String applyTripNo) {
        if (org.apache.commons.lang3.StringUtils.isBlank(applyTripNo)) {
            return null;
        }
        JSONResult<ApplyTripDetailResponse> result = applyTripClient.getApplyTripDetail(applyTripNo);
        if (result == null || result.getData() == null) {
            log.error("查询出差申请单详情异常:" + Optional.ofNullable(result).map(JSONResult::getMsg).orElse("接口无响应"));
            return null;
        }
        return result.getData();
    }

    public boolean updateApplyTripWbsAndCostRemake(ApplyTripUpdateRemakeRequest request) {
        if (request == null) {
            return false;
        }
        JSONResult<Boolean> result = applyTripClient.updateApplyTripWbsAndCostRemake(request);
        if (result == null || result.getData() == null) {
            log.error("修改WBS和成本中心备注:" + Optional.ofNullable(result).map(JSONResult::getMsg).orElse("接口无响应"));
            return false;
        }
        return result.getData();
    }

    public List<ApplyTripPersonResponse> getApplyTripPerson(ApplyTripPersonRequest request) {
        if (request == null) {
            return new ArrayList<>();
        }
        JSONResult<List<ApplyTripPersonResponse>> result = applyTripClient.getApplyTripPerson(request);
        if (result == null || result.getData() == null) {
            log.error("查询出差申请单中出行人:" + Optional.ofNullable(result).map(JSONResult::getMsg).orElse("接口无响应"));
            return new ArrayList<>();
        }
        return result.getData();
    }

    /**
     * 根据行程号查询差标
     *
     * @param trafficId 行程号
     */
    public QueryApplyTripStandardResponse getFlightApplyTripStandard(Long trafficId) {
        if (trafficId == null) {
            return new QueryApplyTripStandardResponse();
        }

        QueryApplyTripStandardRequest request = new QueryApplyTripStandardRequest();
        request.setTrafficId(trafficId);

        JSONResult<QueryApplyTripStandardResponse> response = applyTripClient.queryStandardInfo(request);
        if (response == null || response.getData() == null) {
            log.error("查询出差申请单中差标失败:" + Optional.ofNullable(response).map(JSONResult::getMsg).orElse("接口无响应"));
            return null;
        }

        return response.getData();
    }

	/**
	 * 查询出差申请查询信息
	 * @param request
	 * @return
	 */
	public QueryApplyTripStandardResponse queryStandardInfo(QueryApplyTripStandardRequest request) {
		if (request == null) {
			return null;
		}
		JSONResult<QueryApplyTripStandardResponse> result = applyTripClient.queryStandardInfo(request);
		if (result == null || result.getData() == null) {
			log.error("查询出差申请查询信息:" + Optional.ofNullable(result).map(JSONResult::getMsg).orElse("接口无响应"));
			return null;
		}
		return result.getData();
	}

    /**
     * 更改出差申请单状态为已失效，失效申请单加系统操作记录
     * @param uid
     * @return
     */
    public boolean updateApplyTripStatusExpired(String uid){
        if (StringUtils.isBlank(uid)){
            return  false;
        }
        JSONResult<Boolean> result = applyTripClient.updateApplyTripStatusExpired(uid);
        if (result == null || result.getData() == null) {
            log.warn("更改出差申请单状态为已失效:" + Optional.ofNullable(result).map(JSONResult::getMsg).orElse("接口无响应"));
            return false;
        }
        return result.getData();
    }
}
